library(tidyverse)
library(cowplot)
library(pdftools)
load(here::here('data/data.RData'))

data %>%
  mutate(
    SO = case_when(
      SO == "Austral: Brazilian Journal of Strategy and International Relations" ~ "Austral",
      SO == "Brazilian Journal of International Relations" ~ "BJIR",
      SO == "Conjuntura Austral: Journal of the Global South" ~ "Conjuntura Austral",
      SO == "Estudos Internacionais: Revista de Relações Internacionais da PUC Minas" ~"Estudos Internacionais",
      SO == "Meridiano 47 - Journal of Global Studies" ~ "Meridiano 47",
      SO == "Monções: Revista de Relações Internacionais da UFGD" ~ "Monções",
      SO == "Oikos - Revista de Economia Politica Internacional" ~ "Oikos",
      SO == "Revista Brasileira de Estudos de Defesa" ~ "RBED",
      SO == "Revista Brasileira de Politica Internacional" ~ "RBPI",
      TRUE ~ SO
      
    )
  ) %>% 
  filter(PY < 2022) -> data

data %>%
  separate_rows(GND, sep = ';') %>%
  mutate(GND = str_trim(GND)) %>%
  distinct() %>%
  filter(GND != "NA") -> data_GND


data_GND %>%
  group_by(GND, SO) %>%
  count() %>%
  ungroup() %>%
  group_by(SO) %>%
  mutate(freq =  n / sum(n) * 100) %>%
  ggplot(aes(y = freq, x = GND, fill = GND)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_text(aes(x = GND,
                y = freq,
                label = glue::glue("{round(freq)}%")),
            position = position_dodge(width = 0.9),
            vjust = 1.5,
            fontface = "bold",
            color = "white") +
  scale_fill_manual(values = c('#e34a27','#2384C0')) +
  guides(fill = guide_legend("Gender")) +
  labs(y = "%") +
  scale_y_continuous(limits = c(0,100), breaks = c(0,100)) +
  facet_wrap(~SO, nrow = 3) +
  theme_minimal() +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(10,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    strip.text = element_text(face = "bold"),
    strip.background = element_rect(fill = "#f0f0f0"),
    axis.text.x=element_blank(),legend.position = c(.90, .10)
  ) -> plot9b

plot9b
ggsave(
  plot = plot9b,
  filename = here::here('outputs/figure-05.pdf'),
  device = cairo_pdf,
  dpi = 400
)

pdf_convert(
  pdf = here::here('outputs/figure-05.pdf'),
  filenames = here::here('outputs/figure-05.jpeg'),
  format = "jpeg", dpi = 400
)
